package com.lin.services.impl;

import com.lin.dao.AuthorityDao;
import com.lin.dao.StoreDao;
import com.lin.dao.UserDao;
import com.lin.pojo.Authority;
import com.lin.pojo.Role;
import com.lin.pojo.Store;
import com.lin.pojo.User;
import com.lin.services.AuthorityService;
import com.lin.utils.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Class:AuthorityServiceImpl
 * Package:com.lin.services.impl
 *
 * @author hellcat
 * Date:2020/2/14
 * Description:
 * 🐾
 */
@Service
public class AuthorityServiceImpl implements AuthorityService {
    @Autowired
    private AuthorityDao authorityDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private StoreDao storeDao;

    @Override
    public List<Role> getRoleInfo() {
        return authorityDao.getRoleInfo(ShiroUtil.getUser().getStoreId());
    }

    @Override
    public Map<String, Object> getAuthorityByRoleId(Role role) {
        Map<String, Object> resultMap = new HashMap<>();
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        role.setStoreId(ShiroUtil.getUser().getStoreId());
        List<Authority> allAuthorityList = authorityDao.getAllAuthorityList();
        List<Authority> roleAuthorityList = authorityDao.getAuthorityByRoleId(role.getId(), role.getStoreId());
        for (Authority authority : allAuthorityList) {
            Map<String, Object> map = new HashMap<>();
            map.put("value", authority.getId());
            map.put("title", authority.getDescribe());
//            map.put("children", authority.getChildren());
            ArrayList<Map<String, Object>> childrenList = new ArrayList<>();
            for (Authority childrenAuthority : authority.getChildren()) {
                Map<String, Object> childrenMap = new HashMap<>();
                childrenMap.put("value", childrenAuthority.getId());
                childrenMap.put("pId", childrenAuthority.getpId());
                childrenMap.put("title", childrenAuthority.getDescribe());
                for (Authority value : roleAuthorityList) {
                    if (childrenAuthority.getId().equals(value.getId())) {
                        childrenMap.put("checked", "checked");
                    }
                }
                childrenList.add(childrenMap);
            }
            map.put("children", childrenList);
            for (Authority roleAuthority : roleAuthorityList) {
                if (authority.getId().equals(roleAuthority.getId())) {
                    map.put("checked", "checked");
                }
            }
            list.add(map);
        }
        resultMap.put("code", true);
        resultMap.put("data", list);
        return resultMap;
    }

    @Override
    public Map<String, Object> updateRoleAuthority(String roleId, String checkedBox, String roleName) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<User> userList = userDao.getUserByRoleId(roleId, ShiroUtil.getUser().getStoreId());
            Store store = storeDao.getStoreInfoById(ShiroUtil.getUser().getStoreId());
            for (User user : userList) {
                if (user.getId().equals(store.getContactsId())) {
                    map.put("code", false);
                    map.put("msg", "该角色权限不可修改");
                    return map;
                }
            }
            authorityDao.updateRole(roleId, roleName, ShiroUtil.getUser().getStoreId());
            authorityDao.deleteAuthorityByRoleId(roleId, ShiroUtil.getUser().getStoreId());
            String[] authorityIds = checkedBox.split(",");
            if ("".equals(authorityIds[0].trim())) {
                map.put("code", true);
                return map;
            }
            for (String authorityId : authorityIds) {
                authorityDao.insertRoleAuthority(roleId, authorityId);
            }
            for (User user : userList) {
                ShiroUtil.clearAllCachedInfo(user.getAccountNumber());
            }
            map.put("code", true);
        } catch (Exception e) {
            map.clear();
            map.put("code", false);
            map.put("msg", "修改失败，未知异常");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return map;
    }

    @Override
    public Map<String, Object> deleteRoleById(String roleId) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<User> userList = userDao.getUserByRoleId(roleId, ShiroUtil.getUser().getStoreId());
            if (userList.size() != 0) {
                map.put("code", false);
                map.put("msg", "该角色下存在用户，不可删除");
                return map;
            }
            int i = authorityDao.deleteRoleById(roleId, ShiroUtil.getUser().getStoreId());
            if (i == 1) {
                map.put("code", true);
            }
        } catch (Exception e) {
            map.clear();
            map.put("code", false);
            map.put("msg", "修改失败，未知异常");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return map;
    }

    @Override
    public Map<String, Object> insertRole(String roleName, String authority) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", false);
        map.put("msg", "添加失败");
        if (roleName == null || "".equals(roleName.trim())) {
            map.put("msg", "角色名不能为空");
            return map;
        }
        List<Role> roleList = authorityDao.getRoleByRole(roleName, ShiroUtil.getUser().getStoreId());
        if (roleList.size() != 0) {
            map.put("msg", "角色名已存在");
            return map;
        }
        Role role = new Role();
        role.setRole(roleName);
        role.setStoreId(ShiroUtil.getUser().getStoreId());
        authorityDao.insertRole(role);
        if (role.getId() == null) {
            return map;
        }
        if (authority == null || "".equals(authority.trim())) {
            map.clear();
            map.put("msg", "添加成功");
            map.put("code", true);
            return map;
        }
        String[] authorities = authority.split(",");
        for (String authorityId : authorities) {
            authorityDao.insertRoleAuthority(role.getId().toString(), authorityId);
        }
        map.clear();
        map.put("code", true);
        map.put("msg", "添加成功");
        return map;
    }

}
